package com.woniuxy.lrz.carlproject.service.impl;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.controller.form.shopform.ShopLoginForm;
import com.woniuxy.lrz.carlproject.controller.form.shopform.ShopUpdateForm;
import com.woniuxy.lrz.carlproject.model.Shop;
import com.woniuxy.lrz.carlproject.dao.ShopMapper;
import com.woniuxy.lrz.carlproject.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.ShopDto.ShopDto;
import com.woniuxy.lrz.carlproject.service.dto.ShopDto.ShopListDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 门店表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
@Slf4j
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Resource
    private ShopMapper shopMapper;


    @Override
    public String login(ShopLoginForm shopLoginForm) {
        QueryWrapper<Shop> wrapper = new QueryWrapper<Shop>();
        //密码加密进数据库查询
        String s = DigestUtils.sha256Hex(shopLoginForm.getShopPwd()).toString();
        wrapper.eq("shop_master_phone",shopLoginForm.getShopMasterPhone())
                .eq("shop_pwd",s);
        Shop shop = shopMapper.selectOne(wrapper);
        if (shop == null) {
            throw new AppException("登录失败");
        }
        StpUtil.login(shop.getShopId(), SaLoginConfig.setExtra("shopName", shop.getShopName())
        .setExtra("shopAddress",shop.getShopAddress())
        .setExtra("shopId",shop.getShopId())
        );

        return StpUtil.getTokenValue();
    }

    @Override
    public void updateByShopId(int shopId,ShopUpdateForm shopUpdateForm) {
        UpdateWrapper<Shop> wrapper = new UpdateWrapper<>();
        wrapper.eq("shop_id",shopId)
                .set("shop_name",shopUpdateForm.getShopName())
                .set("shop_phone",shopUpdateForm.getShopPhone())
                .set("shop_address",shopUpdateForm.getShopAddress())
                //.set("shop_open_time",shopUpdateForm.getShopOpenTime())
               // .set("shop_close_time",shopUpdateForm.getShopCloseTime())
                .set("status",shopUpdateForm.getStatus())
                .set("shop_master_name",shopUpdateForm.getShopMasterName())
                .set("shop_master_id_card",shopUpdateForm.getShopMasterIdCard())
                .set("shop_master_phone",shopUpdateForm.getShopMasterPhone());
                //.set("shop_license",shopUpdateForm.getShopLicense())
               // .set("shop_pwd",DigestUtils.sha256Hex(shopUpdateForm.getShopPwd()).toString());
        int update = shopMapper.update(null, wrapper);
        if (update<1){
            throw  new AppException("修改信息失败");
        }
    }

    //根据服务详情id查询门店列表
    public List<ShopListDto> getShopById(Integer serviceId) {
        List<ShopListDto> shops = shopMapper.getShopById(serviceId);
        if(shops==null){
            throw new AppException("该服务没有相关门店信息");
        }
        return shops;
    }
}
